<?php
// +----------------------------------------------------------------------
// | 中犇单商户
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2021 中犇科技 All rights reserved.
// +----------------------------------------------------------------------


namespace app\user\admin;

use app\admin\admin\Base;
use app\user\model\UnsetApply as UnsetApplyModel;
use app\user\model\User;
use service\Format;
use service\ApiReturn;

/**
 * 注销申请控制器
 * @package app\UnsetType\admin
 */
class UnsetApply extends Base
{
    /**
     * 注销申请列表
     * @author 似水星辰 [2630481389@qq.com]
     * @return mixed
     */
    public function index()
    {
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        // 查询
        $map = $this->getMap();
        // 数据列表
        $data_list = UnsetApplyModel::alias('a')
            ->field('a.*')
            ->field('b.user_nickname')
            ->join('user b', 'a.user_id = b.id')
            ->where($map)
            ->order('status', 'asc')
            ->paginate();
        $fields =[
		    ['id', 'ID', '', '', '', ],
            ['user_id', '用户ID', '', '', '', ],
            ['user_nickname', '用户昵称', '', '', '', ],
            ['mobile', '用户手机号', '', '', '', ],
            ['unset_type', '注销类型', '', '', '', ],
            ['unset_reason', '注销原因', '', '', '', ],
            ['status', '状态', 'status', '', ['未审核:default', '审核通过:success', '已拒绝:danger']],
        ];
        return Format::ins() //实例化
            ->setPrimaryKey('id')
            ->addColumns($fields)//设置字段
               ->setRightButtons([
                [
                    'icon' => 'fa fa-send (alias)',
                    'ident' => 'success',
                    'title' => '立即注销',
                    'href' => ['setStatus', ['type' => 'success', "id" => '__user_id__']],
                    'class' => 'btn mr5 btn-success ajax-get confirm',           
                ],
                [
                    'icon' => 'fa fa-times',
                    'ident' => 'refuse',
                    'title' => '拒绝注销',
                    'href' => ['setStatus', ['type' => 'refuse',  "id" => '__user_id__']],
                    'class' => 'btn mr5 btn-danger ajax-get confirm',           
                ],
                [
                    'icon' => 'fa fa-info',
                    'ident' => 'reset',
                    'title' => '恢复使用',
                    'href' => ['setStatus', ['type' => 'reset',  "id" => '__user_id__']],
                    'class' => 'btn mr5 btn-warning ajax-get confirm',           
                ]

            ])   
            ->replaceRightButton(["status"=>[">",0]], "", "success")
            ->replaceRightButton(["status"=>[">",0]], "", "refuse")
            ->replaceRightButton(["status"=>["neq",1]], "", "reset")
            ->setRightButtons($this->right_button)
            ->setData($data_list)//设置数据
            ->fetch();//显示
    }

    public function setStatus($type = 'success',$id = 0)   
    {
        
        empty($id) && $this->error('缺少主键');
        $info = UnsetApplyModel::where('user_id',  $id)->find();
        if(!$info){
            $this->error('数据异常');
        }
       
        switch ($type) {
            case 'success': // 通过注销
                if($info["status"]){
                    $this->error('已处理');
                }
                 $user =  \app\user\model\User::where('id',  $id)->find();
                 UnsetApplyModel::where('user_id',  $id)->update([
                     'status'=> 1,
                     "old_user_nickname"=>$user["user_nickname"],
                     "old_head_img"=>$user["head_img"],
                 ]);
                //账号匿名化，禁止再注册
                \app\user\model\User::where('id',  $id)->update([
                    "is_delete"=>1,
                    "user_nickname"=>"账户已注销",
                    "head_img"=>""
                ]);
                \app\common\model\ApiApptoken::clearUserToken($id);//让客户端下线
                break;
            case 'refuse': // 拒绝注销
                if($info["status"]){
                    $this->error('已处理');
                }
                 UnsetApplyModel::where('user_id',  $id)->setField('status', 2);             
                break;
            case 'reset': // 拒绝注销
                if($info["status"] != 1){
                    $this->error('未注销');
                }
                UnsetApplyModel::where('user_id',  $id)->delete();      
                \app\user\model\User::where('id',  $id)->where("is_delete",1)->update([
                    "is_delete"=>0,
                    "user_nickname"=> $info['old_user_nickname']  ?: '未填写昵称',
                    "head_img"=> $info['old_head_img'],
                ]);
                break;
            default:
                $this->error('非法操作');
                break;
        }
       $this->success("操作成功");
    }
}